<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="700px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="150px">

          <el-form-item label="商家" prop="sponsorId">
            <el-input
              v-if="false"
              v-model="form.sponsorId"
              style="width: 370px;"
            />
            <div v-if="form.sponsorId">
              {{ dialogTableDataSponsorId.showInputValue }}
            </div>
            <!-- 新增组件弹出式表单 -->
            <dialogTable
              :dialogTableData.sync="dialogTableDataSponsorId"
              @select-callback="dialogTableSelectedCallBackSponsorId"
            ></dialogTable>
          </el-form-item>

          <el-form-item label="会员id">
            <el-input v-model="form.memberId" style="width: 370px;" />
          </el-form-item>


          <el-form-item label="姓名" prop="sponsorTeamName">
            <el-input v-model="form.sponsorTeamName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="电话" prop="sponsorTeamPhone">
            <el-input v-model="form.sponsorTeamPhone" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="微信">
            <el-input v-model="form.sponsorTeamWechat" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="职位" prop="sponsorTeamPosition">
            <el-input v-model="form.sponsorTeamPosition" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="备注说明">
            <el-input v-model="form.remark" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns.visible('sponsorName')" prop="sponsorName" label="商家" />
        <el-table-column v-if="columns.visible('sponsorCode')" prop="sponsorCode" label="商家编码" />
        <el-table-column v-if="columns.visible('sponsorTypeMeaning')" prop="sponsorTypeMeaning" label="商家类型" />
        <el-table-column v-if="columns.visible('sponsorPhone')" prop="sponsorPhone" label="商家电话" />
        <el-table-column v-if="columns.visible('sponsorTeamName')" prop="sponsorTeamName" label="姓名" />
        <el-table-column v-if="columns.visible('sponsorTeamPhone')" prop="sponsorTeamPhone" label="电话" />
        <el-table-column v-if="columns.visible('sponsorTeamWechat')" prop="sponsorTeamWechat" label="微信" />
        <el-table-column v-if="columns.visible('sponsorTeamPosition')" prop="sponsorTeamPosition" label="职位" />
        <el-table-column v-if="columns.visible('remark')" prop="remark" label="备注说明" />
        <el-table-column v-if="columns.visible('memberId')" prop="memberId" label="会员id" />
        <el-table-column v-permission="['admin','tanzerSponsorTeam:edit','tanzerSponsorTeam:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudTanzerSponsorTeam from '@/api/tanzer/tanzerSponsorTeam'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import MaterialList from "@/components/material";
import dialogTable from "@/views/components/dialogTable/index.vue";

// crud交由presenter持有
const defaultCrud = CRUD({ title: '商家团队', url: 'api/tanzerSponsorTeam', sort: 'id,desc', crudMethod: { ...crudTanzerSponsorTeam }})
const defaultForm = { id: null, sponsorId: null, memberId: null, sponsorTeamName: null, sponsorTeamPhone: null, sponsorTeamWechat: null, sponsorTeamPosition: null, remark: null, isDel: null, createTime: null, createBy: null, updateBy: null, updateTime: null }
export default {
  name: 'TanzerSponsorTeam',
  components: {dialogTable, pagination, crudOperation, rrOperation, udOperation ,MaterialList},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {

      permission: {
        add: ['admin', 'tanzerSponsorTeam:add'],
        edit: ['admin', 'tanzerSponsorTeam:edit'],
        del: ['admin', 'tanzerSponsorTeam:del']
      },
      rules: {
        sponsorId: [
          { required: true, message: '商家id不能为空', trigger: 'blur' }
        ],
        sponsorTeamName: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        sponsorTeamPhone: [
          { required: true, message: '电话不能为空', trigger: 'blur' }
        ],
        sponsorTeamPosition: [
          { required: true, message: '职位不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'id', display_name: '表ID，主键，供其他表做外键' },
        { key: 'sponsorId', display_name: '商家id' },
        { key: 'memberId', display_name: '会员id' },
        { key: 'sponsorTeamName', display_name: '姓名' },
        { key: 'sponsorTeamPhone', display_name: '电话' }
      ],

      //自定义lov数据源
      dialogTableDataSponsorId: {
        title: "商家信息",
        dialogVisible: false,
        url: "api/tanzerSponsor",
        sort: "id,desc",
        buttonContext: "选择商家",
        showInputValue: "",
        queryTypeOptions: [
          { key: 'sponsorName', display_name: '名称' },
          { key: 'sponsorCode', display_name: '编码' },
        ],
        elTtableColumns: [
          {
            columnProp: "sponsorName",
            columnLabel: "名称"
          },
          {
            columnProp: "sponsorCode",
            columnLabel: "编码"
          },
          {
            columnProp: "sponsorTypeMeaning",
            columnLabel: "类型"
          },
          {
            columnProp: "sponsorPhone",
            columnLabel: "电话"
          }
        ]
      },


    }
  },
  watch: {
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      }else{
        delete this.crud.params.id
        delete this.crud.params.sponsorId
        delete this.crud.params.memberId
        delete this.crud.params.sponsorTeamName
        delete this.crud.params.sponsorTeamPhone
      }
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
    },

    [CRUD.HOOK.afterToEdit](crud, form) {
      this.setInputValue(
        this.dialogTableDataSponsorId,
        "showInputValue",
        form,
        "sponsorId",
        "content",
        0,
        "sponsorName",
        "api/tanzerSponsor",
        {
          page: 0,
          size: 10,
          sort: "id,desc",
          id: form.sponsorId
        }
      );
    },

    //省市区数据源
    dialogTableSelectedCallBackSponsorId(row) {
      this.form.sponsorId = row.id;
      if (this.form.sponsorId) {
        this.dialogTableDataSponsorId.showInputValue = row.sponsorName;
      }
    },
  }
}



</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
</style>
